package com.ruoyi.vote.admin.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.vote.admin.domain.VoteCertificate;
import com.ruoyi.vote.admin.domain.VoteCertificateReceive;
import com.ruoyi.vote.admin.domain.bo.VoteCertificateBo;
import com.ruoyi.vote.admin.domain.vo.VoteCertificateVo;
import com.ruoyi.vote.admin.mapper.VoteCertificateMapper;
import com.ruoyi.vote.admin.mapper.VoteCertificateReceiveMapper;
import com.ruoyi.vote.admin.service.IVoteCertificateService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 活动 - 证书模板Service业务层处理
 *
 * @author 筑巢
 * @date 2023-07-17
 */
@RequiredArgsConstructor
@Service
public class VoteCertificateServiceImpl extends ServiceImpl<VoteCertificateMapper, VoteCertificate> implements IVoteCertificateService {

    private final VoteCertificateMapper baseMapper;

    /**
     * 查询活动 - 证书模板
     */
    @Override
    public VoteCertificateVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询活动 - 证书模板列表
     */
    @Override
    public TableDataInfo<VoteCertificateVo> queryPageList(VoteCertificateBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<VoteCertificate> lqw = buildQueryWrapper(bo);
        Page<VoteCertificateVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询活动 - 证书模板列表
     */
    @Override
    public List<VoteCertificateVo> queryList(VoteCertificateBo bo) {
        LambdaQueryWrapper<VoteCertificate> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<VoteCertificate> buildQueryWrapper(VoteCertificateBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<VoteCertificate> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getName()), VoteCertificate::getName, bo.getName());
        lqw.eq(bo.getPrice() != null, VoteCertificate::getPrice, bo.getPrice());
        lqw.eq(StringUtils.isNotBlank(bo.getPic()), VoteCertificate::getPic, bo.getPic());
        lqw.eq(StringUtils.isNotBlank(bo.getContent()), VoteCertificate::getContent, bo.getContent());
        lqw.eq(bo.getCType() != null, VoteCertificate::getCType, bo.getCType());
        lqw.eq(bo.getStatus() != null, VoteCertificate::getStatus, bo.getStatus());
        return lqw;
    }

    /**
     * 新增活动 - 证书模板
     */
    @Override
    public Boolean insertByBo(VoteCertificateBo bo) {
        VoteCertificate add = BeanUtil.toBean(bo, VoteCertificate.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改活动 - 证书模板
     */
    @Override
    public Boolean updateByBo(VoteCertificateBo bo) {
        VoteCertificate update = BeanUtil.toBean(bo, VoteCertificate.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(VoteCertificate entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除活动 - 证书模板
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
